<template>
<div>
  <el-upload
      class="avatar-uploader"
      action="/api/common/upload"
      :headers="headers"
      :show-file-list="false"
      :on-success="handleBusinessLicenseSuccess"
  >
    <img v-if="path"
        :src="imgSrc"
        class="avatar"
        style="width: 100px; height: 100px;"
     alt=""/>
    <el-icon v-else class="avatar-uploader-icon">
      <Plus />
    </el-icon>
  </el-upload>
</div>
</template>

<script setup>

import {Plus} from "@element-plus/icons-vue";
import {ElMessage} from "element-plus";
import {computed} from "vue";

const path = defineModel()

const headers = {
  token: localStorage.getItem('token')
}

// 上传成功
function handleBusinessLicenseSuccess(response, uploadFiley) {
  if (response.code === 200) {
    path.value= response.data;
    ElMessage({ message: "上传成功", type: "success" });
  } else {
    ElMessage({ message: "上传失败", type: "warning" });
  }
}

let imgSrc = computed(()=>{
  //判断是否http/https开头
  if (path.value.startsWith('http://') || path.value.startsWith('https://')){
    return path
  }
  return "/api/common/download/"+path.value
})



</script>

<style scoped>

</style>
